package com.hx.controller;

import com.hx.poji.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * SpringMvc 方式响应数据
 */
@Controller
public class Controller03 {
    @RequestMapping("/test9")
    public String test9(Model model){
       User user = new User(12,"tom","男","4643");
       model.addAttribute("user",user);
        return "model";
    }


    @RequestMapping("/test8")
    public ModelAndView test8(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("mav");
        mav.addObject("mavKey","mavValue");
        User user = new User(10,"jack","男","454");
        mav.addObject("user",user);
        return mav;
    }






    /**
     * 原生servlet传参
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/test7")
    public void test7(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        try {
            request.setAttribute("requestKey", "reqValues");
            session.setAttribute("sessionKey", "sessionValue");
            request.getServletContext().setAttribute("contextKey", "contextValue");
            request.getRequestDispatcher("pages/servletapi.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 原生servlet传参
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/test6")
    public String test6(HttpServletRequest request, HttpServletResponse response, HttpSession session) {

            request.setAttribute("requestKey", "reqValues");
            session.setAttribute("sessionKey", "sessionValue");
            request.getServletContext().setAttribute("contextKey", "contextValue");
            return "servletapi";
    }
}
